package com.panshi.utils;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

/**
 * 发送邮件工具类
 */
public final class MailUtils {
    private MailUtils(){}
    /**
     * 发送邮件
     * @param email 收件人的邮箱地址
     * @param subject 邮件主题
     * @param emailMsg 邮件内容
     */
    public static void sendMail(String email, String subject, String emailMsg)
            throws MessagingException, javax.mail.MessagingException {
        // 1.[连接发件服务器]创建一个程序与发件人的 发送邮件服务器会话对象 Session
        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");//邮件发送协议
        props.setProperty("mail.host", "smtp.qq.com");//邮件发送服务器的地址（如QQ邮箱的发件服务器地址SMTP服务器: smtp.qq.com）
        props.setProperty("mail.smtp.auth", "true");//指定验证为true

        // 创建验证器
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发件人的用户名（不带后缀的，如QQ邮箱的@qq.com不用写）和授权码(这里一般不使用密码，为避免密码泄露，用授权码代替密码登录第三方邮件客户端)
                //POP3/SMTP的授权码：用于登录第三方邮件客户端的专用密码。  第三方邮件客户端：如这个java程序。
                //授权码iuxmbaxujwgybdfd
                return new PasswordAuthentication("834848771", "gyjnacucdlzhbeca");
            }
        };
        System.out.println(auth);
        Session session = Session.getInstance(props, auth);

        // 2.[创建一封邮件]创建一个Message，它相当于是邮件内容
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("834848771@qq.com")); // 设置发送者的邮箱地址
        message.setRecipient(Message.RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者
        message.setSubject(subject);//邮件主题
        message.setContent(emailMsg, "text/html;charset=utf-8");//设置邮件的内容

        // 3.[发送邮件]创建 Transport用于将邮件发送
        Transport.send(message);
    }

    //随机生成验证码
    public static String createSimpleMail(){
        //  获取6为随机验证码
        String[] letters = new String[] {
                "q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m",
                "A","W","E","R","T","Y","U","I","O","P","A","S","D","F","G","H","J","K","L","Z","X","C","V","B","N","M",
                "0","1","2","3","4","5","6","7","8","9"};
        String stringBuilder ="";
        for (int i = 0; i < 6; i++) {
            stringBuilder = stringBuilder + letters[(int)Math.floor(Math.random()*letters.length)];
        }
        return stringBuilder;
    }
    //初始化随机生成昵称
    public static String createUname(){
        String [] first = new String[] {
                "卑微的","强壮的","懦弱的","狂热的","忧郁的","高大的","勇猛的","有才的","有钱的","聪明的","愚笨的",
                "谨慎的","直率的","坚强的","稳重的","幽默的","随和的","大气的","潇洒的","迷人的","年轻的","勤奋的"
        };
        String [] last = new String []{
                "土豆","地瓜","小天才","狗赞","平头哥","大闸蟹","小龙虾","熊猫","狗熊","河马","长颈鹿",
                "狮子","猎豹","空心菜","藕片","叫花鸡","肯德基","麦丹劳","CoCo","詹姆斯","韦德","Uzi","简自豪",
                "严君泽","史森明","刘志豪","李元浩","刘世宇","洪浩轩","姜承録","高振宁","宋义进","喻文波","王柳羿"
        };
        String stf = first[(int)Math.floor(Math.random()*first.length)];
        String stl = last[(int)Math.floor(Math.random()*last.length)];

        System.out.println(stf+stl);

        return stf+stl;
    }

}
